/*
 * CalculatePoint.java
 *
 * Created on 20 de mayo de 2008, 03:35 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.pragmaticminds.forrest.utils;

import com.pragmaticminds.forrest.geometry.Circle;
import com.pragmaticminds.forrest.geometry.Position;
import com.pragmaticminds.forrest.geometry.Line;
import com.pragmaticminds.forrest.utils.Types.*;

/**
 *
 * @author d157743
 */
public class CalculatePoint {
    
    /**
     * Creates a new instance of CalculatePoint
     */
    public CalculatePoint() {
    }
 
    public static Position calculateNewPoint(Position pos, DirectionT dir, double dist){
            Circle circ = new Circle(pos,dist);
            Position posSolution, posSolution1 = null, posSolution2 = null;
            double dirAng= directionToAngle(dir);
            int sols;
            Line l = Line.makeLineFromPositionAndAngle( pos,dirAng);

            switch (dir){
                case DIR_ILLEGAL: 
                    return(pos); 

                case DIR_NORTH: 
                    return new Position(pos.getPositionX()+dist, pos.getPositionY()); 

                case DIR_NORTHWEST: 
                    sols=l.getCircleIntersectionPoints( circ,posSolution1, posSolution2);
                    if (sols==2) 
                        if (posSolution1.getPositionY()<posSolution2.getPositionY())
                            return(posSolution1);
                        else
                            return(posSolution2);
                    else
                        return(posSolution1);

                case DIR_NORTHEAST:
                    sols=l.getCircleIntersectionPoints( circ,posSolution1, posSolution2);
                    if (sols==2) 

                        if (posSolution1.getPositionY() > posSolution2.getPositionY())
                            return(posSolution1);
                        else
                            return(posSolution2);
                    else
                        return(posSolution1);

                case DIR_CENTER: 
                    return(pos); 

                case DIR_EAST: 
                    return new Position(pos.getPositionX(), pos.getPositionY()+dist); 

                case DIR_WEST: 
                    return new Position(pos.getPositionX(), pos.getPositionY()-dist); 

                case DIR_SOUTHWEST: 
                    sols=l.getCircleIntersectionPoints( circ, posSolution1, posSolution2);
                    if (sols==2) 
                        if (posSolution1.getPositionY()< posSolution2.getPositionY())
                            return(posSolution1);
                        else
                            return(posSolution2);
                    else
                        return(posSolution1);

                case DIR_SOUTHEAST: 
                    sols=l.getCircleIntersectionPoints( circ,posSolution1, posSolution2);
                    if (sols==2) 
                        if (posSolution1.getPositionY()> posSolution2.getPositionY())
                            return(posSolution1);
                        else
                            return(posSolution2);
                    else
                        return(posSolution1);

                case DIR_SOUTH: 
                    return new Position(pos.getPositionX()-dist, pos.getPositionY()); 

                case DIR_MAX: 
                    return(pos); 

                default: 
                    return(pos); 
            }	
     }
 
 
     public static double directionToAngle(DirectionT dir){
            switch (dir){                
                case   DIR_ILLEGAL: return(0);
                case   DIR_NORTH: return(0);
                case   DIR_NORTHWEST: return(-45);
                case   DIR_NORTHEAST: return(45);
                case   DIR_CENTER: return(0);
                case   DIR_EAST: return(90);
                case   DIR_WEST: return(-90);
                case   DIR_SOUTHWEST: return(-135);
                case   DIR_SOUTHEAST: return(135);
                case   DIR_SOUTH: return(180);
                case   DIR_MAX: return(0);
                default: return(0);
            }
     }

     public static Position getDribblePoint(Position posAg, DirectionT dir){
            return calculateNewPoint(posAg, dir, Parameters.getParameter(ParaT.PARA_DIST_DRIBBLE_UTI));
     }
 
    public static Position getShootPoint(Position posAgent, double angPateo){ 
 	Line lAux = Line.makeLineFromPositionAndAngle(posAgent, angPateo);
		
	Position posGol = new Position(55, lAux.getYGivenX(55));
	return(posGol);
    }		

}
